home *** CD-ROM | disk | FTP | other *** search
- Path: fnnews.fnal.gov!usenet
- From: Oleg Krivosheev <kriol>
- Newsgroups: comp.lang.c++
- Subject: vietual data in C++?
- Date: 13 Jan 1996 20:23:51 GMT
- Organization: FERMILAB, Batavia, IL
- Message-ID: <4d94cn$9vo@fnnews.fnal.gov>
- NNTP-Posting-Host: martian.fnal.gov
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.4 sun4c)
- X-URL: news:comp.lang.c++
-
- Hi, Gentlemen
-
- i want to implement something like virtual data (if such an idiom
- is already known, there must be it's own name). Let me show:
-
- i define base class
-
- template <class BaseData> class Base {
-
- ...
-
- // methods
-
- ...
-
- // data
-
- BaseData* pVirtualData;
- };
-
- so now i can inherite concrete classes using different Data classes:
-
- class D1: public Base<Data1> {
- ...
- };
-
- class D2: public Base<Data2> {
- ...
- };
-
- etc...
-
- from the other hand BaseData, Data[i] must represent own hierarchy,
- but really contain only data and must have the same layout:
-
- struct BaseData {
- int i1;
- int i2;
- }
-
- struct Data1: public BaseData {
- // ... only methods are added
- };
-
- struct Data2: public BaseData {
- // ... only methods are added
- };
-
- etc, and i can use concrete classes D1, D2...
-
- int main() {
- D1 d1;
- D2 d2;
-
- cerr << d1.pVirtualData->i1; // prints correspondent i1
- cerr << d1.pVirtualData->i2; // prints correspondent i2
-
- }
-
- So, my questions are:
-
- 1. Is such idiom already known? (under it's own name).
-
- 2. What's wrong with above snippet?
-
- Any comments are greatly appreciated
-
- Sincerely Oleg
-
-